package bTree.LeetCode;

import javax.swing.tree.TreeNode;
import java.util.LinkedList;
import java.util.Queue;

public class 层数最深叶子节点的和 {
    public int deepestLeavesSum(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int sum = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            int path = 0;
            for (int i = 0; i < size; i++) {
                TreeNode peek = queue.peek();
                int val = peek.val;
                path += val;
                if (peek.left!=null){
                    queue.add(peek.left);
                }
                if (peek.right!=null){
                    queue.add(peek.right);
                }
                queue.poll();
            }
            sum=path;
        }
        return sum;
    }
}